package com.firefly.admin.config.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.config.entity.FySystemConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.config.service.FySystemConfigurationService;
import com.firefly.admin.config.vo.SystemConfigurationQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "系统配置接口API")
@RestController
@RequestMapping("/system/configuration")
public class FySystemConfigurationController {
    @Autowired
    private FySystemConfigurationService fySystemConfigurationService;

    /**
     * 分页查询系统配置接口
     *
     * @param systemConfigurationQueryVo 分页查询参数
     * @return 分页列表
     */
    @ApiOperation(value = "分页查询系统配置接口", notes = "分页查询系统配置接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:configuration')")
    @GetMapping("/list")
    public Result<Page<FySystemConfiguration>> getSystemConfigurationList(SystemConfigurationQueryVo systemConfigurationQueryVo) {
        return fySystemConfigurationService.getSystemConfigurationList(systemConfigurationQueryVo);
    }

    /**
     * 获取系统配置信息接口
     *
     * @param configId 配置ID
     * @return 配置信息
     */
    @ApiOperation(value = "获取系统配置信息接口", notes = "通过配置ID获取配置详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId", required = true, value = "配置ID", paramType = "path", dataType = "Long")
    })
    @GetMapping("/{configId}")
    public Result<FySystemConfiguration> getSystemConfiguration(@PathVariable Long configId) {
        return fySystemConfigurationService.getSystemConfiguration(configId);
    }

    /**
     * 系统字典信息编辑接口
     *
     * @param fySystemConfiguration 配置信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统字典信息编辑接口", notes = "系统字典信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.CONFIG_MANAGE, operateType = OperationTypeEnum.SYSTEM_CONFIGURATION, operateDesc = "系统配置编辑")
    @PreAuthorize(value = "hasAuthority('system:configuration:update')")
    @PostMapping("/update")
    public Result<FySystemConfiguration> update(@RequestBody FySystemConfiguration fySystemConfiguration) {
        return fySystemConfigurationService.update(fySystemConfiguration);
    }

    /**
     * 通过配置项KEY获取系统配置值接口
     *
     * @param configKey 配置KEY
     * @return 系统配置值
     */
    @ApiOperation(value = "通过配置项KEY获取系统配置值接口", notes = "通过配置项KEY获取系统配置值接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configKey", value = "配置KEY", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/value")
    public Result<String> getConfigurationValue(String configKey) {
        return fySystemConfigurationService.getConfigurationValue(configKey);
    }
}
